package com.lanou.musicbox.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;
import com.lanou.musicbox.base.BaseActivity;
import com.lanou.musicbox.utils.MyConfig;
import com.lanou.musicbox.utils.VolleySingleton;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.sso.UMQQSsoHandler;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by dllo on 15/11/3.
 */
public class LogoutActivity extends BaseActivity implements View.OnClickListener {
    private ImageView logout_back;
    private CircleImageView logout_image;
    private TextView logout_name, logout_sex, logout_province, logout_place;
    private Button logout_exit;
    private ImageLoader imageLoader;
    private String profile_image_url, screen_name, gender, city, province;
    /**
     * 第三方登陆
     **/
    UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logout_main);
        //id初始化
        logout_back = (ImageView) findViewById(R.id.landing_back);
        logout_image = (CircleImageView) findViewById(R.id.logout_image);
        logout_name = (TextView) findViewById(R.id.logout_name);
        logout_sex = (TextView) findViewById(R.id.logout_sex);
        logout_province = (TextView) findViewById(R.id.logout_province);
        logout_place = (TextView) findViewById(R.id.logout_place);
        logout_exit = (Button) findViewById(R.id.logout_exit);
        logout_back.setOnClickListener(this);
        logout_exit.setOnClickListener(this);
        imageLoader = VolleySingleton.getInstance(this).getImageLoader();
        //参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.


        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "100424468",
                "c7394704798a158208a74ab60104f0ba");
        qqSsoHandler.addToSocialSDK();
    }

    /**
     * 获得缓存里的数据
     * 把所获得数据设置到布局里
     **/
    @Override
    protected void onResume() {
        super.onResume();

        profile_image_url= MyConfig.getSharePreStr(this,"profile_image_url","profile_image_url");
        screen_name= MyConfig.getSharePreStr(this,"screen_name","screen_name");
        gender= MyConfig.getSharePreStr(this,"gender","gender");
        city= MyConfig.getSharePreStr(this,"city","city");
        province= MyConfig.getSharePreStr(this,"province","province");
            logout_name.setText(screen_name);
            logout_sex.setText(gender);
            logout_place.setText(city);
            logout_province.setText(province);
            ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(logout_image, R.mipmap.reigst_head, R.mipmap.reigst_head);
            imageLoader.get(profile_image_url, imageListener);
            Log.i("lanou3g取出数据", screen_name + "---" + gender + "----" + city + "----" + province + "---" + profile_image_url + "");


    }

    private void Logout(final SHARE_MEDIA platform) {
        mController.deleteOauth(this, platform,
                new SocializeListeners.SocializeClientListener() {
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onComplete(int status, SocializeEntity entity) {
                        if (status == 200) {
                            Toast.makeText(LogoutActivity.this, "删除成功.",
                                    Toast.LENGTH_SHORT).show();
                            Log.i("是否成功删除", platform + "-----------");

                        } else {
                            Toast.makeText(LogoutActivity.this, "删除失败",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.landing_back:
                finish();
                break;
            case R.id.logout_exit:
                MyConfig.deleteSharePreAll(LogoutActivity.this, "profile_image_url");
                MyConfig.deleteSharePreAll(LogoutActivity.this, "screen_name");
                Logout(SHARE_MEDIA.QQ);
                finish();
                break;
        }
    }
}
